package com.ruoyi.chatrecord.service.impl;

import com.ruoyi.chat.domain.ChatMessages;
import com.ruoyi.chatrecord.pojo.ChatMessagesOneT;
import com.ruoyi.chatrecord.pojo.MyChatMessage;
import com.ruoyi.chatrecord.service.ChatRecordService;
import com.ruoyi.common.core.domain.AjaxResult;
import dev.langchain4j.data.message.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author WaKy
 * @version 1.0
 */
@Service
public class ChatRecordServiceImpl implements ChatRecordService {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public AjaxResult getAll() {
        // 最终结果
        //ArrayList<Map<Long, List<MyChatMessage>>> result = new ArrayList<>();
        // 将数据从Map封装改为实体类封装，更灵活
        ArrayList<ChatMessagesOneT> result = new ArrayList<>();
        // 判空
        List<ChatMessages> allMessages = mongoTemplate.findAll(ChatMessages.class);
        if (allMessages.isEmpty()) {
            return AjaxResult.error("没有聊天记录");
        }

        for (ChatMessages msg : allMessages) {
            List<MyChatMessage> finalMessages = new LinkedList<>();
            List<ChatMessage> messages = new LinkedList<>();
            // 每一个单独的用户聊天记录(区分不同用户以及同一用户的不同时间的聊天记录)
            messages.addAll(ChatMessageDeserializer.messagesFromJson(msg.getContent()));
            for (ChatMessage message : messages) {
                // 手动判断是什么类型，然后取出来构建成我们的实体类，最终返回
                if (message instanceof SystemMessage) {
                    // 系统消息，不做处理
                   /* MyChatMessage myChatMessage = new MyChatMessage();
                    myChatMessage.setRole("system");
                    myChatMessage.setContent(((SystemMessage) message).text());
                    finalMessages.add(myChatMessage);*/
                } else if (message instanceof UserMessage) {
                    MyChatMessage myChatMessage = new MyChatMessage();
                    myChatMessage.setRole("user");
                    TextContent content = (TextContent) ((UserMessage) message).contents().get(0);
                    myChatMessage.setContent(content.text().substring(7));
                    finalMessages.add(myChatMessage);
                } else if (message instanceof AiMessage) {
                    MyChatMessage myChatMessage = new MyChatMessage();
                    myChatMessage.setRole("ai");
                    myChatMessage.setContent(((AiMessage) message).text());
                    finalMessages.add(myChatMessage);
                }
            }
            ChatMessagesOneT oneT = new ChatMessagesOneT();
            oneT.setChatTime(msg.getChatTime());
            oneT.setMemoryId(msg.getMemoryId());
            oneT.setMessages(finalMessages);
            result.add(oneT);
            //HashMap<Long, List<MyChatMessage>> map = new HashMap<>();
            //map.put(msg.getMemoryId(), finalMessages);
            // result.add(map);
        }
        // 最终返回结果：AjaxResult<List<Map<String(memoryId),List>>>
        return AjaxResult.success(result);
    }
}
